package com.freecloud.bitorrent.domain.server.scheduler;

import cn.hutool.extra.spring.EnableSpringUtil;
import cn.hutool.system.SystemUtil;
import com.freecloud.bitorrent.domain.server.DHTServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时加入DHT网络
 *
 * @Author: maomao
 * @Date: 2022/7/23 5:38 PM
 */
@Slf4j
@Component
@EnableSpringUtil
public class AutoJoinDHTScheduler {

    private final DHTServer dhtServer;

    public AutoJoinDHTScheduler(DHTServer dhtServer) {
        this.dhtServer = dhtServer;
    }

    @Scheduled(fixedDelay = 10 * 1000, initialDelay = 10 * 1000)
    public void doJob() {
        boolean serverDebug = SystemUtil.getBoolean("bt.debug.server", false);
        if (!serverDebug) {
            log.info("自动重新加入 DHT 网络中...");
            dhtServer.joinDHT();
        }
    }
}
